<template>
  <!-- 此处图片用于当视频加载失败时的替换背景图片 -->
  <div class="login-container">
    <div class="login-content">
      <!-- 此处内容放置中间登录模块 -->
      <!-- ///// -->

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
        label-position="left">

        <div class="title-container">
          <h3 class="title">用户登录</h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>

          <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text"
            tabindex="1" auto-complete="on" />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>


          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="Password" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-button type="primary" style="width:100%;margin-bottom:20px;border-color:#DC143C; background-color:#DC143C;"
          @click.native.prevent="handleLogin">Login</el-button>
        <el-row style="text-align: center;">

          <!-- <button>点击跳转2</button> -->
          <el-link :underline="false" class="msg">Don't have account?</el-link>
          <el-button type="text" style="width:5%;margin-bottom:5px;margin-top: -40px;margin-left: 3px;color:white;"
            @click.native.prevent="handleRegister">Register</el-button>

        </el-row>
        <el-row style="text-align: center;">
          <el-link :underline="false" class="msg">More information?</el-link>
          <!-- <el-link class="msg" type="text" @click.native.prevent="handleContactUs">Contact us</el-link> -->
          <a href="../../../static/index.html" style="font-size:13px;">Contact us</a>

        </el-row>
      </el-form>
    </div>

    <div class="videoContainer">
      <video :style="fixStyle" class="fillWidth" playsinline="" autoplay="" muted="" loop="" v-on:canplay="canplay">
        <source src="@/assets/images/login/background.mp4" type="video/mp4" />
      </video>
    </div>

  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'



export default {
  name: 'Login',
  data () {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value))
    //   {
    //     callback(new Error('Please enter the correct user name'))
    //   } else
    //   {
    //     callback()
    //   }
    // }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        // username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    // $route: {
    //   handler: function (route) {
    //     this.redirect = route.query && route.query.redirect
    //   },
    //   immediate: true
    // }
  },
  methods: {

    //视屏播放失败展示背景图片
    canplay () {
      this.vedioCanPlay = true
    },
    //监听屏幕大小实时给播放器设置宽高
    getVideoStyle () {
      window.onresize = () => {
        const windowWidth = document.body.clientWidth
        const windowHeight = document.body.clientHeight
        const windowAspectRatio = windowHeight / windowWidth
        console.log(windowWidth, windowHeight, windowAspectRatio, '屏幕实时大小')
        let videoWidth
        let videoHeight
        if (windowAspectRatio < 0.5625) {
          videoWidth = windowWidth
          videoHeight = videoWidth * 0.5625
          this.fixStyle = {
            height: windowWidth * 0.5625 + 'px',
            width: windowWidth + 'px',
            'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
            'margin-left': 'initial'
          }
        } else {
          videoHeight = windowHeight
          videoWidth = videoHeight / 0.5625
          this.fixStyle = {
            height: windowHeight + 'px',
            width: windowHeight / 0.5625 + 'px',
            'margin-left': (windowWidth - videoWidth) / 2 + 'px',
            'margin-bottom': 'initial'
          }
        }
      }
      window.onresize()
    },


    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            //   this.$router.push({ path: this.redirect || '/' })
            this.$router.push('/dashboard')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleRegister () {
      this.$router.push('/register')
    },
    handleContactUs () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            //   this.$router.push({ path: this.redirect || '/' })
            this.$router.push('/contactUs')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }





  }
}


</script>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  background-size: cover;
  background-attachment: scroll;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  // position: fixed;



  .login-form {

    //width: 50%;
    height: 50%;
    border-radius: 10px;
    //here
    //  display: flex;
    //here
    //display: center;
    // justify-content: center;
    // align-items: center;
    font-size: 20px;
    align-content: center;
    // letter-spacing: 0.5em;
    //透明
    //background-color:rgba(255, 255, 255, 0.7);
    position: relative;
    width: 520px;
    max-width: 100%;
    // padding: 160px 35px 0;
    padding: 15px 35px 15px;
    margin: 0 auto;
    overflow: hidden;

    //  毛玻璃
    backdrop-filter: blur(20px);
    color: #fff;
    box-shadow: 0 0 30px 10px rgba(0, 0, 0, 0.3);


  }



  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 0px 5px 6px 5px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}


.videoContainer {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: -100;
}

.videoContainer:before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  display: block;
  z-index: -1;
  top: 0;
  left: 0;
  background: rgba(25, 29, 34, 0.65);
}

// .fillWidth {
//   width: 100%;
//   height:100%;
// }
</style>


